package auxiliadormatricula.persistencia;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import auxiliadormatricula.logica.NoDisciplina;
import auxiliadormatricula.logica.StatusDisciplina;

public class DBAdapter {
	public static SQLiteDatabase database;
	private DbHelper dbHelper;
	private String[] discCol = {DbHelper.ID_DISC, DbHelper.NOME_DISC, DbHelper.STATUS_DISC};
	private String[] depCol = {DbHelper.ID_DISC_FK, DbHelper.NOME_DEP};
	private static int pointer = 0;

	public DBAdapter(Context context) {
		dbHelper = new DbHelper(context);
	}
	
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		if(getDisciplinas().getCount() == 0){
			addDisciplinas();
		}
		

	}

	public void close() {
		dbHelper.close();
		database.close();
	}
	
	public Cursor getDisciplinas(){
		Cursor cursor = database.rawQuery("select " + DbHelper.ID_DISC + ", " + DbHelper.NOME_DISC + "," + 
				DbHelper.STATUS_DISC + " from " + DbHelper.TABLE_DISC_NOME, null);
		return cursor;
	}
	
	public Cursor getDependencias(){
		Cursor cursor = database.rawQuery("select " + DbHelper.ID_DISC_FK + ", " + DbHelper.NOME_DEP + 
				" from " + DbHelper.TABLE_DEP_NOME, null);
		return cursor;
	}
	
	
	public NoDisciplina getDisciplinaById(int idDisc){
		Cursor cursor = database.query(DbHelper.TABLE_DISC_NOME, discCol, DbHelper.ID_DISC + " = " + idDisc, null,null, null, null);
		cursor.moveToFirst();
		return cursorToNoDisciplina(cursor);
		
	}
	
	public NoDisciplina getDisciplinaByNome(String nome){
		Cursor cursor = database.query(DbHelper.TABLE_DISC_NOME, discCol, DbHelper.NOME_DISC + "='" + nome + "'", null,null, null, null);
		cursor.moveToFirst();
		return cursorToNoDisciplina(cursor);
		
	}
	
	public NoDisciplina getDiscByNomeEStatusAtual(String nome){
		Cursor cursor = database.query(DbHelper.TABLE_DISC_NOME, discCol, DbHelper.NOME_DISC + "='" + nome + "'", null,null, null, null);
		cursor.moveToFirst();
		return cursorToNoDisciplinaEStatus(cursor);
		
	}
	
	
	
	public NoDisciplina cursorToNoDisciplina(Cursor cursorDisc){
		String discNome = cursorDisc.getString(cursorDisc.getColumnIndex(DbHelper.NOME_DISC));
		String status = cursorDisc.getString(cursorDisc.getColumnIndex(DbHelper.STATUS_DISC));
		for (NoDisciplina disciplina : NoDisciplina.values()) {
			if(disciplina.getNome().equals(discNome)){
				return disciplina;
			}
		}

		return null;

	}
	
	public NoDisciplina cursorToNoDisciplinaEStatus(Cursor cursorDisc){
		String discNome = cursorDisc.getString(cursorDisc.getColumnIndex(DbHelper.NOME_DISC));
		String status = cursorDisc.getString(cursorDisc.getColumnIndex(DbHelper.STATUS_DISC));
		for (NoDisciplina disciplina : NoDisciplina.values()) {
			if(disciplina.getNome().equals(discNome)){
				disciplina.setStatus(StatusDisciplina.getStatusByNome(status));
				return disciplina;
			}
		}

		return null;

	}
	
	
	public String[] getDependenciasDaDisc(int idDisc){
		Cursor cursorDep = database.query(DbHelper.TABLE_DEP_NOME, depCol, DbHelper.ID_DISC_FK + " = " + idDisc, null, null, null, null);
		String[] dependencias = new String[10];
		int count = 0;
		while(!cursorDep.isLast()){
			cursorDep.moveToNext();
			String dependencia = cursorDep.getString(cursorDep.getColumnIndex(DbHelper.NOME_DEP));
			dependencias[count++] = dependencia;
		}
		cursorDep.close();
		return dependencias;
	}
	
	public SQLiteDatabase getDatabase() {
		return database;
	}

	private void addDisciplinas(){
		SQLiteDatabase database = DBAdapter.database;
		ContentValues valoresDisc = new ContentValues();
		ContentValues valoresDep = new ContentValues();
		for (NoDisciplina disc : NoDisciplina.values()) {
			valoresDisc.put(DbHelper.NOME_DISC, disc.getNome());
			valoresDisc.put(DbHelper.STATUS_DISC, disc.getStatusTexto());
			long insertId = database.insert(DbHelper.TABLE_DISC_NOME, null, valoresDisc);
			for(String dependencia : disc.getDependenciasTexto()){
				valoresDep.put(DbHelper.ID_DISC_FK, insertId);
				valoresDep.put(DbHelper.NOME_DEP, dependencia);
				database.insert(DbHelper.TABLE_DEP_NOME, null, valoresDep);
			}
		}
		
	}
	
	public void atualizarDisc(NoDisciplina disciplinaLocal){
		ContentValues values = new ContentValues();
		NoDisciplina disc = getDisciplinaByNome(disciplinaLocal.getNome());
		disciplinaLocal.setStatus(StatusDisciplina.getStatusByNome(disc.getStatusTexto()));
		values.put(DbHelper.STATUS_DISC, disc.getStatusTexto() );
		database.update(DbHelper.TABLE_DISC_NOME, values, DbHelper.NOME_DISC  +" ='" + disc.getNome() + "'" , null);
	}

	public void atualizarTodasDisc(){
		for (NoDisciplina disc : NoDisciplina.values()) {
			atualizarDisc(disc);
		}
	}
	
	public void imprimeDisciplinas(){
		System.out.println("cadeiras registradas: " + this.getDisciplinas().getCount());
		System.out.println("dependencias registradas: " + this.getDependencias().getCount());
		Cursor c = this.getDisciplinas();
		while(!c.isLast()){
			c.moveToNext();
			int id = c.getInt(c.getColumnIndex(DbHelper.ID_DISC));
			String nomeDisc = c.getString(c.getColumnIndex(DbHelper.NOME_DISC));
			String status = c.getString(c.getColumnIndex(DbHelper.STATUS_DISC));
			System.out.println("ID: " + id+ " Nome: " + nomeDisc + " Status: " + status +   "\n");
		}
		c.close();
	}
	
	public void imprimeDepByDisc(int idDisc){
		String[] dep = getDependenciasDaDisc(idDisc);
		if(dep.length == 0)return;
		System.out.println("Disciplina: " + getDisciplinaById(idDisc).getNome());
		System.out.println("Dependencias: ");
		for (int i = 0; i < dep.length; i++) {
			System.out.print(dep[i] + "\n");
		}
	}

}
